<?php

/**
 * @author liyi
 * @copyright 2018.12.13
 */

namespace Firegame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

class MakeController extends ApiController {
    
    const GAME_TYPE = 'Firegame';  // 游戏类型

    const CACHE_FIRES_KEY = 'fires';  // 游戏全局缓存键

    const DB_USER_NAME = 'fire_game.users'; // 游戏用户数据表

    const DB_INVITELOG_NAME = 'fire_game.invite_log'; // 游戏邀请数据表

    const FIRES_INVITE = "fires:invite"; // 邀请次数

    const FIRES_QID ='fire:qid:%s:type:%s'; // 用户解锁邀请(弃用)

    const FIRES_SHARE = "fire:share";  // 普通分享到群次数(弃用)

    public function __construct()
    {
        parent::__construct();
    }

    // 排行榜
    public function rankingList()
    {
        $uid = isset($this->uid)?$this->uid:I('post.id');
        $type = I('post.type');

        if(!empty($type)){
            // 杀人榜
            if($type==2){
                $map['status'] = 2;
                $map['score'] = array('neq',0);
                $ret = M('users')->field('id,nickname,level,avatar,score,kill_num')->where($map)->order('kill_num desc')->limit(200)->select();

                $this->response(0, "OK",array('randList'=>$ret));
            }else{
                $this->response(0, "类型不存在");
            }
        }else{
            // 积分榜
            $grade = C('GRADE_NAME'); // 等级列表
            $map['status'] = 2;
            $map['score'] = array('neq',0);
            $ret = M('users')->field('id,nickname,level,avatar,score')->where($map)->order('score desc')->limit(200)->select();

            if(!empty($ret)) {
                foreach($ret as $k=>$v) {
                    $ret[$k]['title'] = $grade[$v['level']];
                }
            }
            $this->response(0, "OK",array('randList'=>$ret));
        }

    }

    
    // 游戏复活
    public function revives()
    {
        $uid = isset($this->uid)?$this->uid:I('post.id');
        $gold = I('gold',0,'int'); // 复活金币
        $res = $this->revive($uid,10);
        if(!$res) {
            $this->response(1, "操作失败");
        }
        $this->response(0, "操作成功");
    }

    // 跟新虚拟货币
    public function setGolds() {

        $uid = isset($this->uid)?$this->uid:I('post.id');
        $gold = I('gold','0','string');
        $diamonds = I('diamonds','0','string');

        if(!empty($gold)) {
            $data['gold'] = $gold;
        } 
        if(!empty($diamonds)) {
            $data['diamonds'] = $diamonds;
        }
        
        $map['id'] = $uid;
        $ret = M('users')->where($map)->save($data);
   
        $this->response(0,"操作成功");
    }
    
    // 分享奖励(版本影响停用~)
    public function shareAwards()
    {
        $shareType = I('shareType',1,'int'); // 分享类型(1首页，2升级 3：解锁分享 4：奖赏中心分享)
        $uid = isset($this->uid)?$this->uid:I('post.id');
        $type = I('type',1,'int');
        $gold = I('gold',0,'int'); // 金币
        $diamonds = I('diamonds',0,'int'); // 金币
        
        $prop = $type == 1 ? $gold : $diamonds;
        $rule = array(1=>5); // 每天分享5次
        $ret = $this->shareAward($shareType,$uid,$type,$prop,$rule);

        if(empty($ret)) {
            $this->response(1, "操作失败");
        }
        $this->response(0, "操作成功");
    }

    // 分享到群奖励（版本影响停用）
    public function shareQun() {
        $shareType = I('shareType',0,'int'); // 奖赏中心奖励 4：分享到群
        $uid = isset($this->uid)?$this->uid:I('post.id');
        $create_time = date('Y-m-d');
        if(empty($shareType)) {
            $this->response(2,'分享到群的类型为空');
        }
        $data = array('uid'=>$uid,'share_type'=>$shareType,'create_time'=>$create_time);
        $ret = M('users_share_log')->add($data);
        if(empty($ret)) {
            Log::write('奖赏中心分享到群失败uid:'.$uid.'|操作类型：'.$shareType,'ERROR');
            $this->response(1, "操作失败");
        }
        $this->response(0, "操作成功");
    }

    //  每日登陆列表
    public function everyDaySign() {

        $map['uid'] = isset($this->uid)?$this->uid:I('post.id');
        $map['reward_type'] = 1;
        //$signNum = M('users')->field('sign_num')->where($map)->find();
        //$total = isset($signNum['sign_num'])?$signNum['sign_num']:'0';// 签到次数
        $sum_user = M('users_draw_log')->where(array('uid'=>$map['id']))->count('uid');// 签到领取次数

        $signList = C('EVERY_DAY'); //签到列表
        $date = date('Y-m-d');// 今日的签到时间

        //$award = $this->getRecord($map['uid'],1,'all',$date); // 签到记录
        $count_log = M('users_draw_log')->where($map)->count('uid');//  签到次数
        // 昨天
        $today = M('users_draw_log')->field('create_time')->where($map)->order('create_time desc')->limit(1)->select();
        $temp = array();
        foreach ( $today as $k =>$v){
            $temp = $v;
        }

        $sign_time=strtotime($temp['create_time']); // 最近一次登录时间
        $int=date('Y-m-d');
        $int=strtotime($int);//5
        $ints=$int+86400;
        $int_s=$int-86400;

        if(!empty($count_log)) {
            //$pidArr = array_column($award,'reward_grid');// 签到天数
            //  领取天数在数组里(0：未满足条件 1:未领取 2：已领取)
            foreach($signList as $k=>$v) {
                if( $k < $count_log  ) {   //昨天或以前即签到过
                    $signList[$k]['status'] =  2;
                }else if( $k > $count_log  ) { //未来未签到
                    if($int<$sign_time&&$sign_time<$ints){
                        $signList[$k]['status'] =  0;//剩余7天
                    }else{
                        if($k-$count_log==1){
                            $signList[$k]['status'] = 1;//今天
                        }else{
                            $signList[$k]['status'] =  0;//剩余7天
                        }
                    }
                }else{
                    $signList[$k]['status'] =  2;//昨天

                }
            }
        }else{
            // 第一天签到
            foreach ($signList as $k=>$v){
               if($k==1){
                   $signList[$k]['status'] = 1;//今天
               }
            }
        }
        Log::write('everyDaySign'.print_r($signList,true),'ERROR');
        $this->response(0, "ok",$signList);
    }

    // 领取每日登陆奖励
    public function getPrize() {

        $uid = isset($this->uid)?$this->uid:I('post.id');// 用户编号
        $diamonds = I('diamonds',0,'int');
        $pid = I('pid',0,'string');//   奖品编号
        $pids = explode(',', $pid);
        $name = I('name','每日登录奖励','string');
        Log::write('getPrize'.$pid,'ERROR');
        /*$pid_data['uid'] = $uid;
        $pid_data['reward_grid'] = array('in',$pids);
        $pid_data['reward_type'] = 1;
        $pid_get = M('users_draw_log')->where($pid_data)->find();

        if($pid_get){
            $this->response(3,'今天奖励已领取');exit;
        }*/

        if(!empty($diamonds)) {  // 这里需要处理一下
            $this->setGold($uid,'inc',2,$diamonds);  // reward_type 1:每日登陆奖励  reward_grid 奖品编号 reward_name 奖品名字
        }
        $insertData = array();
        foreach($pids as $k=>$v) {
            $data = array(
                'uid'=>$uid,
                'reward_type'=>1,// 已领取状态
                'reward_grid'=>$v,// 奖品编号
                'status'=>"1",
                'reward_name'=>$name,// 奖励名称
                'days'=>$v,// 领取天数
                'create_time'=>date('Y-m-d H:i:s'),
                'update_time'=>date('Y-m-d H:i:s')
            );
            $insertData[] = $data;
        }

        $ret = M('users_draw_log')->addAll($insertData);
        if(!$ret) {
            Log::write('领取每日登陆奖励失败','ERROR');
            $this->response(1, "操作失败");
        }
        $this->response(0, "操作成功");
    }

    // 每日任务列表
    public function everyDayTask() {
        Log::write('everyDayTask','ERROR');
        $uid = $this->uid;
        $time = date('Y-m-d');
        $dbname = self::DB_INVITELOG_NAME;
        $dbname2 = self::DB_USER_NAME;

        $list = $this->commUserInvite($uid,$type=1,$dbname,$dbname2,$time);
        $inviteToal = $list['number'];  // 邀请人数

        $taskList = C('EVERY_TASK'); //任务列表

        $award = $this->getRecord($uid,2,'all',$time);

        $pidArr = array();
        if(!empty($award)) {
            $pidArr = array_column($award,'reward_grid');
        }
        foreach($taskList as $k=>$v) {
            if($k <= $inviteToal ) {
                $taskList[$k]['status'] = in_array($v['id'], $pidArr) ? 2 : 1;  // 0：未满足条件 1:未领取 2：已领取   
            }        
        }
        $list['taskList'] = $taskList;
        $this->response(0, "ok",$list);
    }

    // 领取每日任务列表
    public function getTask() {
        Log::write('getTask','ERROR');
        $uid = $this->uid;
        $diamonds = I('diamonds',0,'int');  // 钞票
        $pid = I('pid',0,'int');  // 奖品编号
        $name = I('name','每日任务奖励','string'); // 奖品名称
        $time = date('Y-m-d');
        if(empty($diamonds)) {
            $this->response(1, "钻石为空");
        }

        if(empty($pid)) {
            $this->response(2,'请选择的奖品编号');
        }

        $this->setGold($uid,'inc',2,$diamonds);  // 领取钻石

        $pids = explode(',', $pid);

        $insertData = array();
        foreach($pids as $k=>$v) {
            $data = array('uid'=>$uid,'reward_type'=>2,'reward_grid'=>$v,'reward_name'=>$name,'create_time'=>date('Y-m-d H:i:s'),'update_time'=>date('Y-m-d H:i:s'));
            $insertData[] = $data;
        }

        $ret = M('users_draw_log')->addAll($insertData);
        if(!$ret) {
            $this->response(1, "操作失败");
        }
        $this->response(0, "操作成功");
    }

    // 邀请记录
    public function inviteRecord()
    {
        $uid = $this->uid;
        $invite_uid = I('invite_uid',0,'int'); //邀请人用户
        $type = I('type',1,'int'); // 邀请类型
        $time = date('Y-m-d');
        $dbname = 'fire_game.invite_log';  // 数据表
        $cacheKey = self::CACHE_FIRES_KEY; // 缓存键
        $cacheval = self::FIRES_INVITE; // 缓存值

        $this->commInviteRecord($uid,$invite_uid,$type,$time,$dbname,$cacheKey,$cacheval);

    }

    // 解密数据
    public function cryptData()
    {
        $uid = $this->uid;
        $gameType = self::GAME_TYPE;
        $data = C($gameType);
        $appid = $data['APPID'];
        $sessionKey = I('sessionKey','','string'); // sessionKey
        $encryptedData = I('encryptedData','','string'); // 加密数据
        $iv = I('iv','','string');
        $type = I('type',0,'int');  // 1:缓存当天有效 2：每局游戏有效

        $cacheKey = self::FIRES_QID;   // 用户分享到群缓存键
        $cacheval = self::FIRES_SHARE; // 用户分享成功次数记录
        $cacheKey2 = self::GAME_TYPE;  // 用户缓存键

        $this->commCryptData($uid,$gameType,$type=1,$cacheKey,$sessionKey,$encryptedData,$iv,$cacheKey2,$cacheval);

    }

    // 金币加成列表
    public function coinAddList() {
        Log::write('coinAddList','ERROR');
        $uid = $this->uid;
        $time = date('Y-m-d');

        $dbname = self::DB_INVITELOG_NAME;
        $dbname2 = self::DB_USER_NAME;

        $list = $this->commUserInvite($uid,$type=2,$dbname,$dbname2,$time);

        $this->response(0, "ok",$list);
    }

    // 领取奖赏中心的奖励(有待检查)
    public function getReward() {
        Log::write('getReward','ERROR');
        $uid = $this->uid;
        $rewardList = C('REWARD_LIST'); //奖赏中心列表
        $arr = array_column($rewardList,'rate','id');
        $type = I('type',0,'int');  // 1:正常抽奖 2：重置抽奖
        $diamonds = I('diamonds',0,'int'); // 钻石

        $contion['uid'] = $uid;
        $contion['status'] = 1;
        $contion['reward_type'] = 3; // 奖励中心类型
        $contion['create_time'] = date('Y-m-d');

        if($type == 2) {  // 重新开始新一轮抽奖
            $param['status'] = 0;
            $ret = M('users_draw_log')->where($contion)->save($param);
            if(empty($ret)) {
                Log::write('重置新一轮抽奖失败','ERROR');
            }
        }

        $award = M('users_draw_log')->field('reward_grid')->where($contion)->select();

        if(!empty($award)) {
            $awardId = array_column($award,'reward_grid');
            foreach($rewardList as $k=>$v) { 
                $rewardList[$k]['status'] = in_array($v['id'], $awardId) ? 2 : 1;  // 0：未满足条件 1:未领取 2：已领取
                unset($rewardList[$k]['rate']);    
            }
        }

        if($type == 1) {

            $drawNum = count($award);
            switch ($drawNum) {
                case '1':
                    $this->setGold($uid,'dec',$diamonds,1);  // 每轮第一次抽奖消耗一张奖券
                    break;
                 case '2':
                    $params['uid'] = $uid;
                    $params['status'] = 1;
                    $params['shareType'] = 4;  // 奖赏中心分享群类型
                    $params['create_time'] = date('Y-m-d'); // 第二次分享到群可继续
                    $total = M('users_share_log')->where($params)->count('id');
                    if($total==0) {
                        $this->response(2, "请先分享到群,在抽奖");
                    }
                    break;
                case '3':
                    $this->setGold($uid,'dec',2,4);  // 第三次4颗钻石
                    break;
            }
            
            if(!empty($award)) {
                foreach($awardId as $k=>$v) {
                    if(in_array($v, array_keys($arr))) {
                        unset($arr[$v]);
                    }
                }
            }
            $pid = $this->get_rand($arr);
            $rewardInfo =  $rewardList[$pid];
            unset($rewardInfo['rate']);
            $gold = $rewardInfo['gold'] ? intval($rewardInfo['gold']) : 0;
            $diamonds = $rewardInfo['diamonds'] ? intval($rewardInfo['diamonds']) : 0;

            if($pid >= 7) {
                $this->setGold($uid,'inc',2,$diamonds);  // 领取钻石
            }
            if($pid > 1 && $pid < 7) {
                $this->setGold($uid,'inc',1,$gold);  // 领取金币
            }

            $data = array('uid'=>$uid,'reward_type'=>3,'reward_grid'=>$pid,'reward_name'=>$rewardInfo['name'],'create_time'=>date('Y-m-d'));
            $ret = M('users_draw_log')->add($data);
            if(!$ret) {
                $this->response(1, "操作失败");
            }
            $this->response(0, "Ok",$rewardInfo);
        }

        $this->response(0,"OK",$rewardList);
    }

    // 道具列表（暂时不用）
    public function propList() {
        $uid = $this->uid;
        $map['id'] = $uid;
        $userInfo = M('users')->field('bow_level,skin')->where($map)->find();
        $bowLevel = explode(',',$userInfo['bow_level']);
        $propList = C('PROP_LIST');
        foreach($propList as $k=>$v) {
            $propList[$k]['status'] = in_array($v['id'], $bowLevel) ? 2 : 1; // 0：未满足条件 1:未领取 2：已领取
        }
        $skin = $userInfo['skin'];
        $data = array('propList'=>$propList,'skin'=>$skin);
        $this->response(0,"OK",$data);
    }

    //道具解锁
    public function getProp() {
        $uid = $this->uid;
        $diamonds = I('diamonds',0,'int');  // 钻石
        $bow_level = I('bow_level',1,'int'); //弓箭类型
        $type = I('type',0,'int');  // 1:消耗钻石 2：分享

        if(empty($bow_level)) {
            $this->response(2, "请选择弓箭类型");
        }

        if($type == 1) {
            if(empty($diamonds)) {
                $this->response(3, "钻石不能为空");
            }
            $this->setGold($uid,'dec',2,$diamonds);  // 消耗钻石
        }

        // else{
        //     $res = $this->shareAward(1,$uid);
        //     if(empty($res)) {
        //         $this->response(4,'分享失败');
        //     }
        // }

        $map['id'] = $uid;
        $userInfo = M('users')->field('bow_level')->where($map)->find();
        $bowLevel = $userInfo['bow_level'];
        $bowArr = explode(',', $bowLevel);

        if(in_array($bow_level,$bowArr)) {
            $this->response(3, "恭喜！已经具备该弓箭了");
        }
        
        $param['bow_level'] = $userInfo['bow_level'].','.$bow_level;
        $ret = M('users')->where($map)->save($param);
        if(empty($ret)) {
            $this->response(1, "操作失败");
        }
        $this->response(0,"操作成功");
    }

    // 超能研究所(暂时不用)
    public function getSurvey(){
        $uid = $this->uid;
        $map['id'] = $uid;
        $field = 'id,gold,diamonds,level,skin,bow_level,blood,blood_lv,attack,attack_lv,coin_lv';
        $userInfo = M('users')->field($field)->where($map)->find();
        //$userInfo['coin_attr'] = json_decode($userInfo['coin_attr'],true);

        $userInfo['upgrade'] = array(
            1=>array('icon'=>'升级血量','gold'=>'10','diamonds'=>'5'),
            2=>array('icon'=>'升级攻击力','gold'=>'20','diamonds'=>'10'),
            3=>array('icon'=>'升级金币','gold'=>'30','diamonds'=>'15')
        );
        $this->response(0,"OK",$userInfo);
    }

    // 升级研究所用户属性
    public function upgradeUserAttr() {
        $uid = $this->uid;
        $type = I('type',0,'int'); // 1:升级血量 2：升级攻击 3：升级金币属性
        $gold = I('gold',0,'int');
        $diamonds = I('diamonds',0,'int'); // 钻石

        if(empty($gold) && empty($diamonds)) {
            $this->response(2,'金币或钻石不能为空');
        }

        $propLevel = array(1=>'blood_lv',2=>'attack_lv',3=>'coin_lv');
        $attrLv = $propLevel[$type];

        if(!empty($gold)) {
            $data['gold'] = $gold;
        } 
        if(!empty($diamonds)) {
            $data['diamonds'] = $diamonds;
        }
        
        $map['id'] = $uid;
        $res = M('users')->where($map)->save($data);
        if($res) {
            $contion['id'] = $uid;
            $ret = M('users')->where($contion)->setInc($attrLv,1);
            if(empty($ret)) {
                $this->response(1, "操作失败");
            }
        }
        $this->response(0,"操作成功");
    }

    // 用户皮肤列表
    public function userSkin() {
        $uid = $this->uid;
        $map['id'] = $uid;

        $userSkin = C('USER_ATTR');
        $field = 'id,skin,have_skin,gold,diamonds';
        $userInfo = M('users')->field($field)->where($map)->find();

        $skinId = explode(',', $userInfo['have_skin']);
        foreach($userSkin as $k=>$v) {
            if(in_array($v['id'], $skinId)) {
                $userSkin[$k]['status'] = 1;  // 0 未拥有 1 已拥有
            }
        }
        $data['userInfo'] = $userInfo;
        $data['userSkin'] = $userSkin;
        $this->response(0,'操作成功',$data);
    }

    // 获取用户皮肤
    public function getUserSkin() {
        $uid = $this->uid;
        $map['id'] = $uid;
        $type = I('type',1,'int');  // 1:消耗钻石 2：分享
        $skinid = I('skinid',0,'int'); // 皮肤ID
        $diamonds = I('diamonds',0,'int');

        $field = 'id,skin,have_skin,gold,diamonds';
        $userInfo = M('users')->field($field)->where($map)->find();
        $haveSkin = explode(',', $userInfo['have_skin']);

        if(in_array($skinid,$haveSkin)) {
            $this->response(2,'该皮肤已拥有!');
        }
        
        if($type == 1) {
            if(empty($diamonds)) {
                 $this->response(3,'钻石不能为空');
            }
            $this->setGold($uid,'dec',2,$diamonds);  // 消耗钻石
        }
        
        $param['have_skin'] = $userInfo['have_skin'].','.$skinid;
        $ret = M('users')->where($map)->save($param);
        if(empty($ret)) {
            $this->response(1,'购买操作失败');
        }

     
        $this->response(0,'操作成功');
    }

    // 与电脑对战
    public function fightList() {
        $uid = isset($this->uid)?$this->uid:I('post.id');
        $map['id'] = $uid;
        $map['status'] = 2;
        $data['userInfo'] = M('users')->where($map)->find();// 请战玩家

        if(empty($data['userInfo'])){
            $this->response(2,"游客不能快速匹配");
        }

        $level = $data['userInfo']['level'];
        $num = 2;
        $start = ($level-$num) > 0 ? ($level-$num) : 1;
        $arr = array($start,$level,$level+$num);

        $contion['status'] = 2;
        $contion['level'] = array('in',implode(',',$arr));
        $contion['id'] = array('neq',$uid);
        
        $ret = M('users')->where($contion)->order('rand()')->find();
        if(empty($ret)) {
            $contion2['status'] = 2;
            $contion2['level'] = array('elt',$level);
            $contion2['id'] = array('neq',$uid);

            $ret = M('users')->where($contion2)->order('rand()')->find();
        }
        
        $data['robotInfo'] = $ret;
        $this->response(0,"ok",$data);
    }

    // 记录游戏成绩
    public function addGameResult()
    {
        $uid = $this->uid;  // 用户ID
        $score = I('score',0,'int'); // 积分
        $invite_uid = I('invite_uid',0,'int'); // 对战用户
        $is_win = I('is_win',0,'int'); // 1:胜利 2：失败
        $guide = I('guide','','string'); //留言
        $gold = I('gold',0,'int'); //金币
        $level = I('level',0,'int'); // 用户等级
        $checkpoint = I('checkpoint',0,'int'); // 关卡
        $type = I('type','1','int');  // 1:电脑对战 2：AI对战 3：用户对战
        $status = I('status','0','int'); // 是否复仇AI对战

        $map['id'] = $uid;
        $contion['id'] = $invite_uid;

        // if($type != 1) {
        //     if(empty($score)) {
        //         $this->response(2, "用户积分不能为空");
        //     }
        // }
        
        if($type == 3) {  
            if(empty($invite_uid)) {
                $this->response(4, "对战用户不能为空");
            }
        }
        
        if(!empty($gold)) {
            $winData['gold'] = $gold;
        }
        if(!empty($score)) {
            $winData['score'] = $score;
        }
        if(!empty($level)) {
            $winData['level'] = $level;
        }
        if(!empty($checkpoint)) {         
            $winData['checkpoint'] = $checkpoint;
        }
        if(!empty($type)) {
            $winData['type'] = $type;
        }

        if($type == 1) {
            M('users')->where($map)->save($winData);
            $this->response(0, "操作成功");
        }
    
        if($is_win == 1) { //胜利
            if(empty($guide)) {
                $this->response(3, "用户留言不能为空");
            }

            if($type == 2) {  // 只有打AI才有连赢次数
                $winData['last_win'] = array('exp',"last_win + 1");
                $winData['is_win'] = array('exp','is_win - 1');

            }
           
            M('users')->where($map)->save($winData);

        }else if($is_win == 2) { // 失败

            if( $type == 2 ) {
               $winData['last_win'] = 0;
               $winData['is_win'] = array('exp','is_win + 1');
            }
            M('users')->where($map)->save($winData);
        }
        
        if($type == 2) {  // 仅AI对战处理
            $contions['type'] = 2;
            $contions['uid'] = $uid;
            $contions['invite_uid'] = $invite_uid;
            $contions['is_win'] = 2;
            $info = M('play_log')->where($contions)->find(); 
        }

        if(!empty($info)) {
            $rets = M('play_log')->where($contions)->save(array('is_win'=>$is_win));
            $this->response(0, "操作成功"); 
        }

        if($uid == $invite_uid) {
            $this->response(0, "操作成功");
        }
        $data = array('uid'=>$uid,'invite_uid'=>$invite_uid,'type'=>$type,'is_win'=>$is_win,'guide'=>$guide,'create_time'=>date('Y-m-d H:i:s'));
        $res = M('play_log')->add($data);
        if(empty($res)) {
            $this->response(1, "操作失败"); 
        }
        $this->response(0, "操作成功"); 
    }

    // 获取用户数据
    public function getUserInfo(){
        $invite_uid = I('invite_uid',0,'int'); // 对战用户

        if(empty($invite_uid)) {
            $this->response(1, "请选择对战用户");
        }

        $map['id'] = $invite_uid;

        $ret= M('users')->where($map)->find();

        $this->response(0, "ok",$ret); 
    }

    // 数据跟新接口
    public function upgrade() {
        $uid = $this->uid;
        $skin = I('skin',0,'int');
        $blood_lv = I('blood_lv',0,'int');  // 血量等级
        $attack_lv = I('attack_lv',0,'int'); // 攻击力等级
        $coin_lv = I('coin_lv',0,'int'); // 金币等级
        $gold = I('gold',0,'string'); // 金币
        $diamonds = I('diamonds',0,'string'); // 钻石


        $data = array(
            'skin'=>$skin,
            'blood_lv'=>$blood_lv,
            'attack_lv' => $attack_lv,
            'coin_lv' => $coin_lv,
            'gold' => $gold,
            'diamonds' => $diamonds
        );
        $map['id'] = $uid;
        $userInfo = M('users')->where($map)->find();

        if($skin != $userInfo['skin'] || $blood_lv > $userInfo['blood_lv'] || $attack_lv > $userInfo['attack_lv'] || $coin_lv > $userInfo['coin_lv'] ) {

            $ret = M('users')->where($map)->save($data);
        }

        $this->response(0,'操作成功');
    }

    // 仇人列表
    public function foemanList() {
        $uid = isset($this->uid)?$this->uid:I('post.id');
        echo $uid;
        $map['uid'] = $uid;
        $map['is_win'] = 2;
        $map['type'] = 2;
        //$list = M('play_log')->field('invite_uid,guide')->where($map)->limit(60)->select();
        $list = M('play_log')->field('invite_uid,guide')->where($map)->limit(3)->select();
        $inviteUidArr = array_column($list,'invite_uid');

        $guideArr = array_column($list,'guide','invite_uid');

        $inviteUidStr = implode(',', array_unique($inviteUidArr));
        $contion['id'] = array('in',$inviteUidStr);
        $ret = M('users')->field('id,avatar,nickname,gold,diamonds')->where($contion)->select();
        if(!empty($ret)) {
            foreach($ret as $k=>$v) {
                $ret[$k]['guide'] = $guideArr[$v['id']];
            }
        }
        $this->response(0, "ok",$ret);
    }

    // 增加杀人次数 测试
    public static function add_killNum(){
        $str['id'] = I('post.id');
        $arr['kill_num'] = I('post.num');
        $res = M('users')->where($str)->save($arr);
        echo M('users')->getLastSql();
    }

    public function get_users(){
        $time = date("Y-m-d"); //今天
        $endTime = $time.' 23:59:59';
        $map['update_time']  = array('between',"$time,$endTime");
        $data = M('users')->where($map)->select();
        echo M('users')->getLastSql();
    }

    // 获取登录日志
    public function get_users_day(){
        $time = I('param.time')?I('param.time'):date("Y-m-d"); //今天 格式2018-12-17
        $endTime = $time.' 23:59:59';
        $map['update_time']  = array('between',"$time,$endTime");
        $map['type']  = 1;
        $visitor = M('total_log')->where($map)->count();
        $map2['update_time']  = array('between',"$time,$endTime");
        $map2['type']  = 3;
        $users = M('total_log')->where($map2)->count();
        $array = array('游客：'=>$visitor,'注册用户'=>$users);
        //echo M('users')->getLastSql();
        echo "游客：".$visitor;
        echo '</br>';
        echo "注册用户：".$users;
    }


}